import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-11-11
 * Time: 20:11
 */
@WebServlet("/httpservletreq")
public class HttpServletreq extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        sb.append(req.getProtocol()); //返回协议名称和版本
        sb.append("<br>");
        sb.append(req.getMethod()); //返回HTTP请求的方法
        sb.append("<br>");
        sb.append(req.getRequestURI()); //返回从协议名开始直到HTTP请求中的query string
        sb.append("<br>");
        sb.append(req.getContextPath()); //返回HTTP请求的URI部分
        sb.append("<br>");
        sb.append(req.getQueryString()); //返回请求URL中的query string部分
        sb.append("<br>");
        Enumeration<String> parameter = req.getParameterNames(); //返回URL中query string的所有key
        while (parameter.hasMoreElements()) {
            String key = (String)parameter.nextElement(); //获取query string的指定key的value
            String value = req.getParameter(key);
            sb.append(key + ": " + value + "<br>");
        }
        Enumeration<String> enumeration = req.getHeaderNames(); //返回请求Header中的所有key
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement(); //获取Header中指定key的value
            String value = req.getHeader(key);
            sb.append(key + ": " + value + "<br>");
        }
        sb.append(req.getCharacterEncoding()); //返回请求主体使用的字符编码
        sb.append("<br>");
        sb.append(req.getContentType()); //返回请求主体的MIME类型
        sb.append("<br>");
        sb.append(req.getContentLength()); //返回请求主体body部分的长度
        resp.setContentType("test/html; charset=utf8"); //告诉浏览器我们返回的数据的数据类型
        resp.getWriter().write(sb.toString()); //将StringBuider对象中的内容返回给浏览器
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StringBuilder sb = new StringBuilder();
        sb.append(req.getProtocol()); //返回协议名称和版本
        sb.append("<br>");
        sb.append(req.getMethod()); //返回HTTP请求的方法
        sb.append("<br>");
        sb.append(req.getRequestURI()); //返回从协议名开始直到HTTP请求中的query string
        sb.append("<br>");
        sb.append(req.getContextPath()); //返回HTTP请求的URI部分
        sb.append("<br>");
        sb.append(req.getQueryString()); //返回请求URL中的query string部分
        sb.append("<br>");
        Enumeration<String> parameter = req.getParameterNames(); //返回URL中query string的所有key
        while (parameter.hasMoreElements()) {
            String key = (String)parameter.nextElement(); //获取query string的指定key的value
            String value = req.getParameter(key);
            sb.append(key + ": " + value + "<br>");
        }
        Enumeration<String> enumeration = req.getHeaderNames(); //返回请求Header中的所有key
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement(); //获取Header中指定key的value
            String value = req.getHeader(key);
            sb.append(key + ": " + value + "<br>");
        }
        sb.append(req.getCharacterEncoding()); //返回请求主体使用的字符编码
        sb.append("<br>");
        sb.append(req.getContentType()); //返回请求主体的MIME类型
        sb.append("<br>");
        sb.append(req.getContentLength()); //返回请求主体body部分的长度
        resp.setContentType("test/html; charset=utf8"); //告诉浏览器我们返回的数据的数据类型
        resp.getWriter().write(sb.toString()); //将StringBuider对象中的内容返回给浏览器
    }

}
